package com.lineage.echo;

import java.util.Iterator;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.LinkedBlockingQueue;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class PacketHc implements Runnable {
  private static final Log _log = LogFactory.getLog(PacketHc.class);
  
  private final Queue<byte[]> _queue;
  
  private final PacketHandlerExecutor _handler;
  
  private final ClientExecutor _executor;
  
  public PacketHc(ClientExecutor executor) {
    this._executor = executor;
    this._queue = (Queue)new ConcurrentLinkedQueue<>();
    this._handler = new PacketHandler(this._executor);
  }
  
  public PacketHc(ClientExecutor executor, int capacity) {
    this._executor = executor;
    this._queue = (Queue)new LinkedBlockingQueue<>(capacity);
    this._handler = new PacketHandler(this._executor);
  }
  
  public void requestWork(byte[] data) {
    this._queue.offer(data);
  }
  
  public void run() {
    try {
      while (this._executor.get_socket() != null) {
        for (Iterator<byte[]> iter = (Iterator)this._queue.iterator(); iter.hasNext(); ) {
          byte[] decrypt = iter.next();
          iter.remove();
          this._handler.handlePacket(decrypt);
          Thread.sleep(1L);
        } 
        Thread.sleep(10L);
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      this._queue.clear();
    } 
  }
}
